package aex.service;



import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import aex.dao.ArticleCategoryDao;
import aex.dao.ArticleDao;
import aex.dao.PuserDao;
import aex.domain.Article;
import aex.domain.ArticleCategory;
import aex.domain.Puser;
import aex.domain.ShowBox;
import aex.domain.ShowPoint;


/**
 * 用于为页面提供显示数据
 * @author JiangYouYao
 *
 */
@Transactional(readOnly = false)
@Service(ShowViewService.SERVICE_NAME)
public class ShowViewService  {

	
	public static final String SERVICE_NAME="aex.service.ShowViewService";
	/**
	 * 以注解的形式让spring为我们注入dao的实现类
	 */
	@Resource(name = ShowBoxService.SERVICE_NAME)
	public ShowBoxService sbService;
	
	@Resource(name = ArticleService.SERVICE_NAME)
	public ArticleService artService;

	
	@Resource(name = ArticleDao.SERVICE_NAME)
	public ArticleDao arDao;
	
	@Resource(name = ArticleCategoryDao.SERVICE_NAME)
	public ArticleCategoryDao categoryDao;

	@Resource(name = PuserDao.SERVICE_NAME)
	public PuserDao puserDao;
	
	
	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	public ArticleCategoryService acService;
	
	public ShowViewService() {
		
	}
	
	/**
	 * 获取指定展位，指定数目的文章个数
	 * @param showBoxName
	 * @param count
	 * @return
	 */
	public List<Article> getShowBoxArticle(String showBoxName,Integer count){
		ShowBox sbBox = sbService.findBoxByName(showBoxName);
		Set<ShowPoint> sps=sbBox.getShowPoints();
		List<Article> list=new ArrayList<Article>();
		int index =0;
		for (ShowPoint showPoint : sps) {
			list.add(artService.findByID(showPoint.getSpTargetId()));
			index++;
			if((count!=null&&count<index)||	//index超出用户预期跳出
					showPoint.getShowBox().getSbCapacity()<index){//index超出容量跳出
				break;
			}
		}
		return list;
	}

	
	
	/**
	 * 获取最新发布的几条文章啊
	 * @param pageSize
	 * @return
	 */
	public List<Article> getNewArticle(int pageSize){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arTime", "desc");
		return arDao.findCollectionByPage(" ", null, orderby, 1, pageSize);
	}
	
	/**
	 * 获取访问量最大的文章
	 * @param pageSize
	 * @return
	 */
	public List<Article> getHotArticle(int pageSize){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arViewCount", "desc");
		return arDao.findCollectionByPage(" ", null, orderby, 1, pageSize);
	}
	
	
	
	
	/**
	 * 获取用户关注度最高的几个小组
	 * @param pageSize
	 * @return
	 */
	public List<ArticleCategory> getTopCategory( int pageSize ){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.acUserNum", "desc");
		return categoryDao.findCollectionByPage("and tName.acName <>'官方小组'  ", null, orderby, 1, pageSize);
	}
	
	/**
	 * 获取最新注册的几个用户
	 * @param pageSize
	 * @return
	 */
	public List<Puser> getNewUser(int pageSize){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.puTime", "desc");
		return puserDao.findCollectionByPage(" ", null, orderby, 1, pageSize);
	}
	
	
	
	
	/**
	 * 获取积分最多的几个用户
	 * @param pageSize
	 * @return
	 */
	public List<Puser> getTopScoreUser(int pageSize){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.puScore", "desc");
		return puserDao.findCollectionByPage(" ", null, orderby, 1, pageSize);
	}
	
	
	
	
	
	/**
	 * 获取粉丝最多的几个用户
	 * @param pageSize
	 * @return
	 */
	public List<Puser> getTopFansUser(int pageSize){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.puFans", "desc");
		return puserDao.findCollectionByPage(" ", null, orderby, 1, pageSize);
	}
	
	/**
	 * 获取关注别人最多的用户
	 * @param pageSize
	 * @return
	 */
	public List<Puser> getTopAttentionUser(int pageSize){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.puAttention", "desc");
		return puserDao.findCollectionByPage(" ", null, orderby, 1, pageSize);
	}
	
	
	
	
	
	
	
	
	/**
	 * 获取所以的小组
	 * @return
	 */
	public List<ArticleCategory> getAllCategory(){
		return acService.findAllNoPage();
	}
	
	
	
	
	
	

}
